package com.hzdl.apple.controller.cartCotroll;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzdl.apple.bean.User;
import com.hzdl.apple.bean.cart.CartItem;
import com.hzdl.apple.bean.cart.Order;
import com.hzdl.apple.bean.cart.OrderItem;
import com.hzdl.apple.service.cartService.CartService;
import com.hzdl.apple.service.cartService.OrderService;
import com.hzdl.apple.utils.DruidUtils;
import com.hzdl.apple.utils.HttpUtils;
import com.hzdl.apple.utils.DateUtils;

/**
 * 订单处理
 */
@WebServlet("/user/order/*")
public class OrderController extends HttpServlet {

	/**
	 * 
	 */
	private CartService cartService = new CartService();
	private OrderService orderService = new OrderService();

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		getServletContext().setAttribute("path", getServletContext().getContextPath());
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String baseUri = req.getRequestURI();
		User user = (User) req.getSession().getAttribute("user");
		Connection conn = null;
		try {
			conn = DruidUtils.dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		if (baseUri.contains("/addOrder")) {// 添加订单
			try {
				List<CartItem> cartItems = cartService.getAllCarts(user.getUID());

				// 生成订单
				Order order = new Order();
				order.setOrderTime(DateUtils.getNowTime());
				order.setUID(user.getUID());
				order.setUAddress(user.getUAddress());
				order.setUPhoneNum(user.getUPhoneNum());
				order.setShr(user.getUName());
				order.setType(1);// 订单状态为未支付-默认状态
				// 开启手动事务
				conn.setAutoCommit(false);

				orderService.addOrder(order, conn);
				int OID = orderService.getOID(conn);
				// 生成订单项
				for (CartItem c : cartItems) {
					OrderItem orderItem = new OrderItem();
					orderItem.setGoodsName(c.getGoodsName());
					orderItem.setAllPrice(c.getAllPrice());
					orderItem.setGoodsID(c.getGoodsID());
					orderItem.setGoodsPrice(c.getGoodsPrice());
					orderItem.setNums(c.getNums());
					orderItem.setOID(OID);

					orderService.addOrderItem(orderItem, conn, user.getUID());
				}
				cartService.deleteAll(user.getUID(), conn);
				conn.commit();
				order.setOID(OID);
				req.getSession().setAttribute("qiao", 1);//新增订单
				req.getSession().setAttribute("neworder",order);//新增订单放入缓存中
				HttpUtils.forward("cos/order.jsp", req, resp);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (baseUri.contains("/showOrders")) {
			List<Order> orders = orderService.getOrder(user.getUID());
			req.getSession().setAttribute("orders", orders);
			HttpUtils.forward("cos/order.jsp", req, resp);
		} else if (baseUri.contains("/toShxx")) {
			int OID = Integer.parseInt(req.getParameter("oid"));
			req.getSession().setAttribute("oid", OID);
			HttpUtils.forward("cos/shxx.jsp", req, resp);
		} else if (baseUri.contains("/changeOTypeB")) {// 确认购买状态改为4-取消订单(√)
			int OID = Integer.parseInt(req.getParameter("oid"));
			orderService.changType(OID, 4);
			HttpUtils.forward("cos/cart.jsp", req, resp);
		} else if (baseUri.contains("/tuikuan")) {// 确认购买状态改为5-申请退款(√)
			int OID = Integer.parseInt(req.getParameter("oid"));
			orderService.changType(OID, 5);
//			req.setAttribute("tips", "退款申请中...");
			HttpUtils.forward("cos/jumpWEB.jsp", req, resp);
		} else if (baseUri.contains("/forPay")) {// 确认购买状态改为2-支付需确认(√)
			int OID = Integer.parseInt(req.getParameter("oid"));
//			req.getSession().setAttribute("oid", OID);
			orderService.changType(OID,2);
			req.setAttribute("tips", "支付成功！宝贝正在飞奔而来...");
			HttpUtils.forward("cos/payWEB.jsp", req, resp);
		} else if (baseUri.contains("/querenshouhuo")) {// 确认收货状态为3-订单完成(√)
			int OID = Integer.parseInt(req.getParameter("oid"));
			orderService.changType(OID,3);
//			req.setAttribute("tips", "收货成功，欢迎再次购买！");
			HttpUtils.forward("cos/jumpWEB2.jsp", req, resp);
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
